home *** CD-ROM | disk | FTP | other *** search
/ PC Graphics Unleashed / PC Graphics Unleashed.iso / ch01 / vesa_s3.asm < prev    next >
Assembly Source File  |  1993-12-06  |  15KB  |  505 lines

  1. ;--------------------------------------------------------------------------
  2. ; This is file VESA_S3.ASM
  3. ;
  4. ; Copyright (C) 1991 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  5. ; Copyright (C) 1992 Csaba Biegl, 820 Stirrup Dr, Nashville, TN 37221
  6. ; Copyright (C) 1992 Shayne Hughes, UC Davis, CA 95616
  7. ;
  8. ; This file is distributed under the terms listed in the document
  9. ; "copying.dj", available from DJ Delorie at the address above.
  10. ; A copy of "copying.dj" should accompany this file; if not, a copy
  11. ; should be available from where this file was obtained.  This file
  12. ; may not be distributed without a verbatim copy of "copying.dj".
  13. ;
  14. ; This file is distributed WITHOUT ANY WARRANTY; without even the implied
  15. ; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. ;--------------------------------------------------------------------------
  17.  
  18. ; NOTE: this file was formerly called STEALTH.ASM
  19. ; It seems that for once the industry is succeeding in manufacturing
  20. ; compatible graphics boards, as this driver, orginally written for a
  21. ; Diamond Stealth board seems to work unchanged on Orchid F1280 and
  22. ; similar S3 based boards, AND EVEN local bus S3 boards.
  23. ; Thanks to Shayne Hughes, we now have a paging routine for this
  24. ; driver. This allows direct memory access to the video RAM - although
  25. ; GRX is not using it.
  26.  
  27. _TEXT    segment byte public 'CODE'
  28. _TEXT    ends
  29. DGROUP  group    _TEXT,_DATA,_BSS
  30.     assume  cs:_TEXT,ds:DGROUP
  31. _DATA    segment word public 'DATA'
  32. d@    label    byte
  33. d@w    label    word
  34. _DATA    ends
  35. _BSS    segment word public 'BSS'
  36. b@    label    byte
  37. b@w    label    word
  38. _BSS    ends
  39. _TEXT    segment byte public 'CODE'
  40.     assume  cs:_TEXT,ds:DGROUP
  41.  
  42. include grdriver.inc
  43.  
  44. ;--------------------------------------------------------------------------
  45. ; DRIVER HEADER
  46. ;  The following entries MUST match the structure and constant
  47. ;  declarations in the file 'grdriver.h' of the GRX graphics library
  48. ;  The mode word should contain the following bitfields:
  49. ;     - the GRD_NEW_DRIVER bit set for any new format driver
  50. ;     - the adapter type field should be specified
  51. ;     - the memory size field should be specified
  52. ;     - the paging mode field should be specified
  53. ;  The mode set routine will OR in the plane bitfield as it will
  54. ;  change when different color number modes are requested.
  55. ;--------------------------------------------------------------------------
  56.  
  57.     dw    offset mode_set_routine
  58.     dw    offset paging_routine
  59. mode_W  dw    GRD_NEW_DRIVER+GRD_VGA+GRD_1024K+GRD_NO_RW
  60. ;
  61. ; The 'def_xx' fields are filled in by go32 from the corresponding
  62. ; fields of the 'GO32' environment variable
  63. ;
  64. def_tw  dw    80        ; text width
  65. def_th  dw    25        ; text height
  66. def_gw  dw    320        ; graphics width
  67. def_gh  dw    200        ; graphics height
  68. def_nc  dw    16        ; graphics colors
  69.     dw    offset driver_init_routine
  70.     dw    offset text_mode_table
  71.     dw    offset graphics_mode_table
  72.  
  73. ;
  74. ; Biggest text and graphics sizes
  75. ;
  76. Max_TW  equ    80
  77. Max_TH  equ    50
  78. Max_GWn equ    640        ; non interlaced!!!
  79. Max_GHn equ    480
  80. Max_GW  equ    640        ; may be interlaced
  81. Max_GH  equ    480
  82.  
  83.  
  84. ;--------------------------------------------------------------------------
  85. ; TABLE OF SUPPORTED TEXT MODES
  86. ;    - keep sorted by size
  87. ;    - end with an all 0 entry
  88. ;    - BIOS field = 0xff disables it
  89. ;    - fields:
  90. ;        width,  height, colors, BIOS#+  setup_procedure_index*256
  91. ;--------------------------------------------------------------------------
  92. text_mode_table        label word
  93.     dw    80,    25,    2,    007h +  00000h
  94.     dw    40,    25,    16,    001h +  00000h
  95.     dw    80,    25,    16,    003h +  00000h
  96.     dw    80,    28,    16,    003h +  00400h  ; 80x25 + reload 14 row font
  97.     dw    80,    50,    16,    003h +  00600h  ; 80x25 + reload 8 row font
  98.     dw    132,    25,    16,    109h +  00000h  ; VESA 109h mode
  99.     dw    132,    28,    16,    109h +  00400h  ; VESA 109h mode (132x25) + reload 14 row font
  100.     dw    132,    44,    16,    10ah +  00000h  ; VESA 10ah mode
  101.     dw    132,    50,    16,    109h +  00600h  ; VESA 109h mode (132x25) + reload 8 row font
  102.     dw    0,    0,    0,    000h +  00000h
  103.  
  104. ;--------------------------------------------------------------------------
  105. ; TABLE OF SUPPORTED GRAPHICS MODES
  106. ;    - keep sorted first by colors then by size
  107. ;    - end with an all 0 entry
  108. ;    - BIOS field = 0xff disables it
  109. ;    - fields:
  110. ;        width,  height, colors, BIOS#+  setup_procedure_index*256
  111. ;--------------------------------------------------------------------------
  112. graphics_mode_table    label word
  113.     dw    320,    200,    16,    00dh +  00000h
  114.     dw    640,    200,    16,    00eh +  00000h
  115.     dw    640,    350,    16,    010h +  00000h
  116.     dw    640,    480,    16,    012h +  00000h
  117.     dw    800,    600,    16,    002h +  00100h  ; VESA 102h mode
  118.     dw    320,    200,    256,    013h +  00000h
  119.     dw    640,    480,    256,    001h +  00200h  ; VESA S3 201h mode
  120.     dw    800,    600,    256,    003h +  00200h  ; VESA S3 203h mode
  121.     dw     1024,    768,    256,    005h +  00200h  ; VESA S3 205h mode
  122.     dw    0,    0,    0,    000h +  00000h
  123.  
  124. ;--------------------------------------------------------------------------
  125. ; TABLE OF SPECIAL SETUP PROCEDURES
  126. ;  You may need such procedures for:
  127. ;     -- reloading fonts on standard EGA or VGA for
  128. ;     higher resolution text modes
  129. ;     -- enable HiColor mode of some Super VGAs
  130. ;     -- Handle the parameter passing conventions of the VESA BIOS
  131. ;     -- put VGA into 256 color plane mode ("MODE X")
  132. ;     -- etc...
  133. ;  There should be one entry in the table for every non-zero
  134. ;  'setup_procedure_index' in the text and graphics mode tables.
  135. ;  The first entry in the table belongs to index 100h, and so on.
  136. ;  The special setup procedure is invoked via a near call.
  137. ;
  138. ;  Entry: DI=address of the mode record from the text or graphics
  139. ;      table to set up.
  140. ;
  141. ;  Exit:  Adapter configured
  142. ;      BX=driver mode word as it should be returned by the mode set
  143. ;         routine. Typically it involves picking up the mode word
  144. ;         from the header and OR-ing in the appropriate bitplane mode
  145. ;         bitfield. (This is not needed for text modes)
  146. ;      AX, CX, DX, SI can be trashed, PRESERVE DI!!!!
  147. ;
  148. ;  NOTE: This runs in real mode, but don't mess with the segment registers.
  149. ;--------------------------------------------------------------------------
  150. special_setup_table    label word
  151.     dw    offset  VESA_SVGA_mode_set    ; for 01xxh modes
  152.     dw    offset  VESA_S3_mode_set    ; for 02xxh accelerated S3 modes
  153.     dw    0
  154.     dw    offset  VGA_28_row_mode_set    ; for 28 row text modes
  155.     dw    offset  VGA_28_row_mode_set    ; for 28 row VESA text modes
  156.     dw    offset  VGA_50_row_mode_set    ; for 50 row text modes
  157.     dw    offset  VGA_50_row_mode_set    ; for 50 row VESA text modes
  158.  
  159. ;
  160. ; Routine to set up VGA 50 row mode
  161. ; interface is described above
  162. ;
  163. VGA_50_row_mode_set    proc    near
  164.     mov    ax,WORD PTR [di+6]    ; get base mode number
  165.     and    ah,1            ; clear setup proc index
  166.     je    std_50_mode
  167.     mov    bx,ax
  168.     mov    ax,4f02h
  169.     int    10h
  170.     cmp    ax,004fh        ; VESA error ?
  171.     je    mode_50_done
  172.     mov    ax,3            ; VESA error -- set 80x25 mode
  173. std_50_mode:
  174.     int    10h
  175. mode_50_done:
  176.     xor    bx,bx
  177.     mov    ax,1112h        ; load 8x8 font
  178.     int    10h
  179.     ret
  180. VGA_50_row_mode_set    endp
  181.  
  182. ;
  183. ; Routine to set up VGA 28 row mode
  184. ; interface is described above
  185. ;
  186. VGA_28_row_mode_set     proc     near
  187.     mov    ax,WORD PTR [di+6]    ; get base mode number
  188.     and    ah,1            ; clear setup proc index
  189.     je    std_28_mode
  190.     mov    bx,ax
  191.     mov    ax,4f02h
  192.     int    10h
  193.     cmp    ax,004fh        ; VESA error ?
  194.     je    mode_28_done
  195.     mov    ax,3            ; VESA error -- set 80x25 mode
  196. std_28_mode:
  197.     int    10h
  198. mode_28_done:
  199.     xor    bx,bx
  200.     mov    ax,1111h        ; load 8x14 font
  201.     int    10h
  202.     ret
  203. VGA_28_row_mode_set     endp
  204.  
  205. ;
  206. ; Routine to set up VESA SVGA modes
  207. ; only used for 16 color text and graphics modes
  208. ; interface is described above
  209. ;
  210. VESA_SVGA_mode_set    proc    near
  211.     mov    ax,4f02h
  212.     mov    bx,WORD PTR [di+6]
  213.     int    10h
  214.     mov    bx,0ffffh        ; error code
  215.     cmp    ax,004fh
  216.     jne    VESA_SVGA_error
  217.     mov    bx,mode_W
  218.     or    bx,GRD_4_PLANES
  219. VESA_SVGA_error:
  220.     ret
  221. VESA_SVGA_mode_set    endp
  222.  
  223. ; Routine to set up VESA S3 modes
  224. ; only used for 256 color graphics modes
  225. ; interface is described above
  226. ;
  227. VESA_S3_mode_set    proc    near
  228.     mov    ax,4f02h
  229.     mov    bx,WORD PTR [di+6]
  230.     int    10h
  231.     mov    bx,0ffffh        ; error code
  232.     cmp    ax,004fh
  233.     jne    VESA_S3_error
  234.     mov    bx,mode_W
  235.     xor    bx,(GRD_VGA XOR GRD_S3)
  236.     or    bx,GRD_8_PLANES
  237. VESA_S3_error:
  238.     ret
  239. VESA_S3_mode_set    endp
  240.  
  241.  
  242. ;--------------------------------------------------------------------------
  243. ; DRIVER INIT ROUTINE
  244. ;  called once after the driver is loaded
  245. ;  may do one or more of the followings:
  246. ;    - check for proper board type
  247. ;    - check amount of RAM on board, and:
  248. ;    -- update word in header to reflect correct amount
  249. ;    -- disable modes in the tables for which there is not enough RAM
  250. ;    - check for special equipment (HiColor DAC, etc...)
  251. ;
  252. ;  Entry: nothing
  253. ;
  254. ;  Exit:  AX=status:
  255. ;       non-zero: OK,
  256. ;       0: something went wrong (e.g. wrong adapter, etc..)
  257. ;      BX,CX,DX may be trashed
  258. ;
  259. ;  NOTE: This runs in real mode, but don't mess with the segment registers.
  260. ;--------------------------------------------------------------------------
  261. driver_init_routine    proc    far
  262.     push    di
  263.     push    es
  264.     sub    sp,256
  265.     mov    di,sp
  266.     mov    ax,ss
  267.     mov    es,ax
  268.     mov    ax,4f00h
  269.     int    10h        ; check for VESA BIOS
  270.     cmp    ax,004fh
  271.     jne    init_error
  272.     cmp    BYTE PTR es:[di+0],'V'
  273.     jne    init_error
  274.     cmp    BYTE PTR es:[di+1],'E'
  275.     jne    init_error
  276.     cmp    BYTE PTR es:[di+2],'S'
  277.     jne    init_error
  278.     cmp    BYTE PTR es:[di+3],'A'
  279.     jne    init_error
  280.     mov    ax,1        ; OK, VESA BIOS found
  281.     jmp    init_done
  282. init_error:
  283.     xor    ax,ax
  284. init_done:
  285.     add    sp,256
  286.     pop    es
  287.     pop    di
  288.     ret
  289. driver_init_routine    endp
  290.  
  291.  
  292. ;--------------------------------------------------------------------------
  293. ; MODE SET ROUTINE
  294. ;  sets up a text or graphics mode as close as possible to the one
  295. ;  reguested by the user with regard to number of colors and size.
  296. ;
  297. ;  Entry: AX=mode selection
  298. ;     0 = 80x25 text
  299. ;     1 = default text
  300. ;     2 = text CX cols by DX rows
  301. ;     3 = biggest text
  302. ;     4 = 320x200 graphics
  303. ;     5 = default graphics
  304. ;     6 = graphics CX width by DX height
  305. ;     7 = biggest non-interlaced graphics
  306. ;     8 = biggest graphics
  307. ;     9 = graphics BX colors, CX width by DX height
  308. ;
  309. ;  Exit: BX=driver mode flag
  310. ;     CX=width (in pixels or characters)
  311. ;     DX=height
  312. ;
  313. ;  NOTE: This runs in real mode, but don't mess with the segment registers.
  314. ;     YOU SHOULD NOT NEED TO CHANGE THIS ROUTINE AS IT IS PRETTY
  315. ;     MUCH TABLE DRIVEN
  316. ;--------------------------------------------------------------------------
  317. mode_set_routine    proc    far
  318.     push    ds
  319.     push    di
  320.     push    si
  321.     mov    si,cs
  322.     mov    ds,si
  323.     cmp    ax,9
  324.     jbe    DoIt
  325.     jmp    Exit
  326. DoIt:    add    ax,ax
  327.     mov    si,ax
  328.     jmp    WORD PTR mode_set_table[si]
  329. mode_set_table  label    word
  330.     dw    offset mode_0
  331.     dw    offset mode_1
  332.     dw    offset mode_2
  333.     dw    offset mode_3
  334.     dw    offset mode_4
  335.     dw    offset mode_5
  336.     dw    offset mode_6
  337.     dw    offset mode_7
  338.     dw    offset mode_8
  339.     dw    offset mode_9
  340. mode_0: mov    si,offset text_mode_table    ; 80x25 text
  341.     mov    bx,def_nc
  342.     mov    cx,80
  343.     mov    dx,25
  344.     jmp    Lookup
  345. mode_1: mov    si,offset text_mode_table    ; default text
  346.     mov    bx,def_nc
  347.     mov    cx,def_tw
  348.     mov    dx,def_th
  349.     jmp    Lookup
  350. mode_2: mov    si,offset text_mode_table    ; CX*DX text
  351.     mov    bx,def_nc
  352.     jmp    Lookup
  353. mode_3: mov    si,offset text_mode_table    ; biggest text
  354.     mov    bx,def_nc
  355.     mov    cx,Max_TW
  356.     mov    dx,Max_TH
  357.     jmp    Lookup
  358. mode_4: mov    si,offset graphics_mode_table    ; 320x200 graphics
  359.     mov    bx,def_nc
  360.     mov    cx,320
  361.     mov    dx,200
  362.     jmp    Lookup
  363. mode_5: mov    si,offset graphics_mode_table    ; default graphics
  364.     mov    bx,def_nc
  365.     mov    cx,def_gw
  366.     mov    dx,def_gh
  367.     jmp    Lookup
  368. mode_6: mov    si,offset graphics_mode_table    ; CX*DX graphics
  369.     mov    bx,def_nc
  370.     jmp    Lookup
  371. mode_7: mov    si,offset graphics_mode_table    ; biggest non-interlaced gr
  372.     mov    bx,def_nc
  373.     mov    cx,Max_GWn
  374.     mov    dx,Max_GHn
  375.     jmp    Lookup
  376. mode_8: mov    si,offset graphics_mode_table    ; biggest graphics
  377.     mov    bx,def_nc
  378.     mov    cx,Max_GW
  379.     mov    dx,Max_GH
  380.     jmp    Lookup
  381. mode_9: mov    si,offset graphics_mode_table    ; CX*DX graphics w/ BX colors
  382. ;
  383. ; At this point:
  384. ;   SI points to the table to search (text or graphics)
  385. ;   BX has colors
  386. ;   CX has width
  387. ;   DX has height
  388. ;
  389. Lookup: xor    ax,ax                ; last color number seen
  390. Find_C: cmp    [si+4],ax            ; last color number == this?
  391.     je    Same_C
  392.     jb    Prev_C                ; end of table -- use last color
  393.     cmp    BYTE PTR [si+6],0ffh        ; valid entry ?
  394.     je    Prev_C                ; not -- use last color
  395.     mov    ax,[si+4]            ; record color number
  396.     mov    di,si                ; start of entries w/ this color
  397.     cmp    ax,bx                ; enough colors ?
  398.     jae    Find_S
  399. Same_C: add    si,8
  400.     jmp    Find_C
  401. Prev_C: or    ax,ax                ; found any color at all?
  402.     je    Exit
  403. ;
  404. ; At this point:
  405. ;   DI points into the table to the first entry with the desired color
  406. ;      number (either it has enough colors or it is the highest color
  407. ;      number supported by the driver). Additionally, at least the
  408. ;      first (= smallest size) entry for this color is valid (has a
  409. ;      valid BIOS number).
  410. ;   AX has the color number adjusted for the driver
  411. ;   CX has width
  412. ;   DX has height
  413. ;
  414. Find_S: cmp    [di+4],ax            ; still the same color #?
  415.     jne    Prev_S
  416.     cmp    BYTE PTR [di+6],0ffh        ; valid entry ?
  417.     je    Prev_S
  418.     cmp    [di],cx
  419.     jb    Next_S
  420.     cmp    [di+2],dx
  421.     jae    GotIt
  422. Next_S: add    di,8
  423.     jmp    Find_S
  424. Prev_S: sub    di,8
  425. ;
  426. ; At this point:
  427. ;   DI points to the table entry we want to set up
  428. ;
  429. GotIt:  mov    ax,[di+6]            ; BIOS mode number
  430.     or    ah,ah                ; special ?
  431.     je    doBIOS
  432.     mov    al,ah
  433.     xor    ah,ah
  434.     dec    ax
  435.     add    ax,ax
  436.     mov    si,ax
  437.     call    WORD PTR special_setup_table[si]
  438.     jmp    RetVal
  439. doBIOS: int    10h
  440.     mov    bx,GRD_1_PLANE
  441.     cmp    WORD PTR [di+4],2        ; 2 colors ?
  442.     je    doFLAG
  443.     mov    bx,GRD_4_PLANES
  444.     cmp    WORD PTR [di+4],16        ; 16 colors ?
  445.     je    doFLAG
  446.     mov    bx,GRD_8_PLANES
  447.     cmp    WORD PTR [di+4],256        ; 256 colors ?
  448.     je    doFLAG
  449.     mov    bx,GRD_16_PLANES
  450.     cmp    WORD PTR [di+4],32768        ; 32K colors ?
  451.     je    doFLAG
  452.     mov    bx,GRD_PLANE_MASK        ; something is wrong!!
  453. doFLAG: or    bx,mode_W
  454. RetVal: mov    cx,[di]
  455.     mov    dx,[di+2]
  456. Exit:    pop    si
  457.     pop    di
  458.     pop    ds
  459.     ret
  460. mode_set_routine    endp
  461.  
  462.  
  463. ;--------------------------------------------------------------------------
  464. ; PAGING ROUTINE
  465. ;
  466. ;  Entry: AH=read page
  467. ;      AL=write page
  468. ;
  469. ;  Exit: VGA configured.
  470. ;     AX,BX,CX,DX,SI,DI may be trashed
  471. ;
  472. ;  NOTE: This runs in protected mode!  Don't mess with the segment registers!
  473. ;     This code must be relocatable and may not reference any data!
  474. ;--------------------------------------------------------------------------
  475.     assume  ds:nothing, es:nothing
  476.  
  477. paging_routine  proc    far
  478.     mov    cl,al                ; save for later
  479.     mov    ax,4838h            ; Unlock S3
  480.     mov    dx,3d4h
  481.     out    dx,ax
  482.     mov    al,31h                ; turn on  paging
  483.     out    dx,al
  484.     inc    dx
  485.     in    al,dx
  486.     or    al,1
  487.     out    dx,al
  488.     dec    dx
  489.     mov    al,35h                ; index CRT Reg. Lock
  490.     out    dx,al
  491.     inc    dx                ; read CRT Reg. Lock
  492.     in    al,dx
  493.     and    al,0f0h                ; update with new bank
  494.     or    al,cl
  495.     out    dx,al                ; write CRT Reg. Lock
  496.     mov    ax,0038h            ; Lock S3
  497.     dec    dx
  498.     out    dx,ax
  499.     ret
  500. paging_routine  endp
  501.  
  502. _TEXT    ends
  503.     end
  504.  
  505.